using System;
using System.Windows.Forms;
using Moonlite.ExtensionApi.ControlInterfaces;
using Moonlite.ExtensionApi.GlobalInterfaces;
using Moonlite.Platform.General;
using Moonlite.Platform.Project;
using Moonlite.Platform.Text.Project;

namespace Moonlite.UI.Shell.Wizards
{
    public partial class CreateProjectWizard : Form, ICreateProjectWizard
    {
        private TreeNode JassNode { get; set; }
        private TreeNode GalaxyNode { get; set; }

        private string ValidateError { get; set; }
        public IProject Project { get; set; }
        public IProjectTemplate Template { get; set; }
        public bool Result { get; set; }

        private void OnOKClick(object sender, EventArgs e)
        {
            if (ValidateOptions() == true)
            {
                Project = new Project() { Name = textEditName.Text, Path = textEditPath.Text, Description = memoDescription.Text, Version = "1.0.0.0", Files = new ProjectFileCollection() };
                Template = treeViewProjectTemplates.SelectedNode.Tag as IProjectTemplate;
                Project.Type = Template.Category == ProjectTemplateCategory.Galaxy ? ProjectType.Galaxy : ProjectType.Jass;

                Result = true;
                Close();
            }
            else
            {
                Result = false;
                ShowError(ValidateError);
            }
        }

        private void OnCloseClick(object sender, EventArgs e)
        {
            Result = false;
            Close();
        }

        private bool ValidateOptions()
        {
            bool result = true;

            if (treeViewProjectTemplates.SelectedNode == null || treeViewProjectTemplates.SelectedNode == JassNode ||
                treeViewProjectTemplates.SelectedNode == GalaxyNode)
            {
                ValidateError = "Invalid template selected";
                result = false;
            }

            if (string.IsNullOrEmpty(textEditName.Text) == true)
            {
                ValidateError = "You must enter a name for the project";
                result = false;
            }

            if (string.IsNullOrEmpty(textEditPath.Text) != true)
            {
                return result;
            }

            ValidateError = "You must enter a path for the project";
            result = false;

            return result;
        }

        private static void ShowError(string message)
        {
            MessageBox.Show(String.Format("Error while validating input: {0}", message), "Validating error",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void PopulateTemplateList()
        {
            treeViewProjectTemplates.Nodes.Clear();
            GalaxyNode.Nodes.Clear();
            JassNode.Nodes.Clear();
            foreach (IProjectTemplate projectTemplate in ProjectTemplateManager.Templates)
            {
                TreeNode projectTemplateNode = new TreeNode(projectTemplate.Name) { Tag = projectTemplate };

                if (projectTemplate.Category == ProjectTemplateCategory.Galaxy)
                {
                    GalaxyNode.Nodes.Add(projectTemplateNode);
                }
                else if (projectTemplate.Category == ProjectTemplateCategory.Jass)
                {
                    JassNode.Nodes.Add(projectTemplateNode);
                }
            }

            treeViewProjectTemplates.Nodes.Add(JassNode);
            treeViewProjectTemplates.Nodes.Add(GalaxyNode);
        }

        private void OnBrowseClick(object sender, EventArgs e)
        {
            folderBrowserDialog.SelectedPath = textEditPath.Text;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textEditPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void OnTreeViewNodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            labelTemplateDescription.Text = e.Node != GalaxyNode && e.Node != JassNode ? (e.Node.Tag as IProjectTemplate).Description : "";
        }

        public CreateProjectWizard()
        {
            InitializeComponent();
            Global.Instance.RegisterService(this, GetType().GetInterface("ICreateProjectWizard"));
            labelTemplateDescription.Text = "";
            textEditPath.Text = Global.GetProjectsFolder();

            JassNode = new TreeNode("Jass");
            GalaxyNode = new TreeNode("Galaxy");

            PopulateTemplateList();
        }
    }
}